//
//  HomePageViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/4.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "HomePageViewController.h"
#import <Masonry.h>
#import "HomePageTopView.h"
#import "CommonData.h"
#import "HomePageTopTableViewCell.h"
#import "HomePageCardTableViewCell.h"
#import "UIAlertController+Evan.h"
#import "DKProgressHUD.h"
#import "taskHomePageService.h"
#import "MeMessageViewController.h"

@interface HomePageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)    UITableView *tableView;
@property (nonatomic,strong)    dataMinerMainInfo   *modelMinerMain;
@property (nonatomic,strong)    NSArray<dataMinerMainList*>*   mainMinerList;

@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"首页";
    self.view.backgroundColor = COLOR_Background_black;
    
    [self initUI];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setBlackStatus:NO];
    self.hidesBottomBarWhenPushed = NO;
    if (self.tabBarController.tabBar.hidden == YES) {
        self.tabBarController.tabBar.hidden = NO;
    }
    if (self.navigationController.navigationBar.hidden == NO) {
        self.navigationController.navigationBar.hidden = YES;
    }
    if ([UserCenter shareCenter].isLogined) {
        [self loadData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - loadData

-(void)loadData{
    
}

-(void)initUI{
    //上面黑色View
    
    [self tableView];
}

-(void)noticeClick:(UIButton*)sender{
    [self.navigationController pushViewController:[MeMessageViewController new] animated:YES];
    
}

#pragma mark - tableivew


-(UITableView *)tableView{
    if (!_tableView) {
        self.automaticallyAdjustsScrollViewInsets = NO;
        _tableView = [[UITableView alloc]init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.top.and.right.mas_equalTo(self.view);
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        }];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        //定义显示默认cell的横线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate

//定义每一个section有多少row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

//定义有多少section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

//定义CellRow的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            return 520;
        }
            break;
            
        default:
            break;
    }
    return 180 * [self.mainMinerList count] +20;
}

//定义section的Footer的view视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = COLOR_Background;
    return view;
}

//定义section的Footer的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}


//定义section的header的view视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = COLOR_Background;
    return view;
}

//定义section的header的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

//主体cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        NSString *strIndexCell = [NSString stringWithFormat:@"HomePageTopTableViewCell_%ld_%ld",(long)indexPath.section,(long)indexPath.row];
        HomePageTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIndexCell];
        if (!cell) {
            [self.tableView registerClass:[HomePageTopTableViewCell class] forCellReuseIdentifier:strIndexCell];
            cell = [tableView dequeueReusableCellWithIdentifier:strIndexCell forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.viewTop.btnInviteFriend addTarget:self action:@selector(btnInviteFriendClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        cell.viewTop.labelStatus.text = @"算力运行中...";
        [cell.viewTop imgDbottom];
        cell.viewTop.labelSL.text =  [NSString stringWithFormat:@"算力 %@",self.modelMinerMain.currEfficiency];
        cell.viewTop.labelGET.text =  [NSString stringWithFormat:@"GTE %@",self.modelMinerMain.totalProfit];
        [cell.viewTop.btnIntroduce addTarget:self action:@selector(btnIntroduceClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        return cell;
    }
    NSString *strIndexCell = [NSString stringWithFormat:@"HomePageCardTableViewCell_%ld_%ld",(long)indexPath.section,(long)indexPath.row];
    HomePageCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIndexCell];
    if (!cell) {
        [self.tableView registerClass:[HomePageCardTableViewCell class] forCellReuseIdentifier:strIndexCell];
        cell = [tableView dequeueReusableCellWithIdentifier:strIndexCell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
    }
    [cell setDataArray:self.mainMinerList pmView:self];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self.navigationController pushViewController:[MeMessageViewController new] animated:YES];
    }
   
}


@end
